package com.lmqb.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmqb.bean.SysMenu;
import com.lmqb.bean.SysRole;
import com.lmqb.bean.SysRolemenu;
import com.lmqb.bean.SysUser;
import com.lmqb.bean.SysUserLoginLog;
import com.lmqb.bean.SysUserOperationLog;
import com.lmqb.dao.SysMenuMapper;
import com.lmqb.dao.SysRoleMapper;
import com.lmqb.dao.SysRolemenuMapper;
import com.lmqb.dao.SysUserLoginLogMapper;
import com.lmqb.dao.SysUserMapper;
import com.lmqb.dao.SysUserOperationLogMapper;
import com.lmqb.entry.LeftMenu;
import com.lmqb.page.PageTool;
import com.lmqb.page.SysUserLogPager;
import com.lmqb.page.SysUserLoginPager;
import com.lmqb.service.SysUserService;
import com.lmqb.util.StrUtil;
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUserMapper sysUserDao;
	
	@Autowired
	private SysUserLoginLogMapper adminLoginDao;
	
	@Autowired
	private SysRolemenuMapper roleMenuDao;
	
	@Autowired
	private SysMenuMapper sysMenuDao;
	
	@Autowired
	private SysRoleMapper sysRoleDao;
	
	@Autowired
	private SysUserOperationLogMapper sysUserLogDao;
	
	//@Override
	public SysUser SelectByName(String username) {
		SysUser sysUser=sysUserDao.selectByName(username);
		return sysUser;
	}

	//@Override
	public void updateLogin(String ip, SysUser sysUser2) {
		SysUser sysUser=new SysUser();
		sysUser.setUserId(sysUser2.getUserId());
		sysUser.setLastIp(ip);
		sysUser.setLastLoginTime(StrUtil.timeToInt(new Date()));
		sysUser.setLoginTimes(sysUser2.getLoginTimes()+1);
		sysUserDao.updateByPrimaryKeySelective(sysUser);
		//添加管理员登录记录
		SysUserLoginLog adminLog=new SysUserLoginLog();
		adminLog.setLoginIp(sysUser.getLastIp());
		adminLog.setLoginName(sysUser2.getLoginName());
		adminLog.setLoginTime(sysUser.getLastLoginTime());
		adminLog.setRealName(sysUser2.getRealName());
		adminLog.setUserId(sysUser2.getUserId());
		adminLoginDao.insert(adminLog);
	}

	//生成做菜单栏
	public List<LeftMenu> selectRoleMenu(SysUser sysUser) {
		List<SysRolemenu> list=roleMenuDao.selectByRoleId(sysUser.getRoleId());
		List<LeftMenu> leftMenus=new ArrayList<LeftMenu>();
		for(int i=0;i<list.size();i++){
			SysMenu menu=sysMenuDao.selectByPrimaryKey(list.get(i).getMenuId());
			if(menu!=null&&!"check_type_one".equals(menu.getMenuCode())
					&&!"check_type_two".equals(menu.getMenuCode())
					&&!"check_type_three".equals(menu.getMenuCode())
					&&!"has_cuishou".equals(menu.getMenuCode())
					&&!"check_agent".equals(menu.getMenuCode())){//去掉控制审核权限的菜单
				if(menu.getMenuStatus()==1){//有效的才会显示，无效的不显示
					if(menu.getParentId()==(byte)0){//找到父类
						LeftMenu leftMenu=new LeftMenu();
						leftMenu.setSysMenu(menu);
						leftMenus.add(leftMenu);
					}else{
						for(int j=0;j<leftMenus.size();j++){
							//找到对应的父菜单
							if(menu.getParentId()==leftMenus.get(j).getSysMenu().getMenuId()){
								leftMenus.get(j).getMenuList().add(menu);
							}
						}
					}
				}
			}
		}
		
		return leftMenus;
	}

	//@Override
	public List<SysUser> selectAll(SysUser admin) {
		List<SysUser> list=sysUserDao.selectAll(admin);
		for(int i=0;i<list.size();i++){
			//角色、时间
			SysRole sysRole=sysRoleDao.selectByPrimaryKey(list.get(i).getRoleId());
			list.get(i).setRoleName(sysRole.getRoleName());
			list.get(i).setLastLoginTimeD(StrUtil.intToTime(list.get(i).getLastLoginTime()));
		}
		return list;
	}

	//添加管理员
	public void insert(SysUser sysUser1,SysUser sysUser) {
		sysUser1.setLoginTimes(0);
		sysUser1.setUserStatus((byte)1);
		sysUser1.setUserPwd(StrUtil.Get_MD5(sysUser1.getUserPwd()));
		sysUserDao.insert(sysUser1);
		//添加管理员操作记录
		SysUserOperationLog log=new SysUserOperationLog();
		log.setAddTime(StrUtil.timeToInt(new Date()));
		log.setLoginName(sysUser.getLoginName());
		log.setObjectId(sysUser1.getUserId()+"");
		log.setOperationCate((byte)4);
		log.setOperationContent("添加管理员。"+sysUser1.toString());
		log.setRealName(sysUser.getRealName());
		log.setUserId(sysUser.getUserId());
		log.setTableName("sys_user");
		sysUserLogDao.insert(log);
	}

	//@Override
	public SysUser selectById(Integer userId) {
		SysUser sysUser=sysUserDao.selectByPrimaryKey(userId);
		return sysUser;
	}

	//修改管理员
	public void update(SysUser sysUser1) {
		if(StrUtil.isNull(sysUser1.getUserPwd())){
			sysUser1.setUserPwd(null);
		}else{
			sysUser1.setUserPwd(StrUtil.Get_MD5(sysUser1.getUserPwd()));
			
		}
		sysUserDao.updateByPrimaryKeySelective(sysUser1);
//		//添加管理员操作记录
//		SysUserOperationLog log=new SysUserOperationLog();
//		log.setAddTime(StrUtil.timeToInt(new Date()));
//		log.setLoginName(sysUser.getLoginName());
//		log.setObjectId(sysUser1.getUserId()+"");
//		log.setOperationCate((byte)4);
//		log.setOperationContent("修改管理员。"+sysUser1.toString());
//		log.setRealName(sysUser.getRealName());
//		log.setUserId(sysUser.getUserId());
//		log.setTableName("sys_user");
//		sysUserLogDao.insert(log);
	}

	//@Override
	public void insertLog(SysUserOperationLog log) {
		sysUserLogDao.insert(log);
		
	}

	//@Override
	/*
	 * 获取管理员操作日志
	 * 
	 * */
	public SysUserLogPager selectLogPager(SysUserLogPager pagerModel) {
		//获取管理员操作日志
		List<SysUserOperationLog> list=sysUserLogDao.selectAll(pagerModel);
		for(int i=0;i<list.size();i++){
			list.get(i).setAddTimeD(StrUtil.intToTime(list.get(i).getAddTime()));
		}
		pagerModel.setList(list);
		int count = sysUserLogDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	//@Override
	public SysUserLoginPager selectLoginPager(SysUserLoginPager pagerModel) {
		// 获取管理员登录日志
		List<SysUserLoginLog> list=adminLoginDao.selectAll(pagerModel);
		for(int i=0;i<list.size();i++){
			list.get(i).setLoginTimeD(StrUtil.intToTime(list.get(i).getLoginTime()));
		}
		pagerModel.setList(list);
		int count = adminLoginDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	//@Override
	public void updateAdmin(SysUser admin) {
		sysUserDao.updateByPrimaryKeySelective(admin);
		
	}

}
